/**
  ******************************************************************************
  * @file    log-util.c
  * @author  dragonchern
  * @version V2.2.0
  * @date    09-15-2014
  * @brief   调试和记录日志
  * @project linux client
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */

#include "log-util.h"




char        *LogPath = CLIENT_NAME ".log";
static int   semid = -1;
static FILE *fp = NULL;								// 日志文件指针

/**
  * @brief  打开日志
  * @param  日志路径
  * @retval 成功为TRUE，否则FALSE
  */
BOOL open_log()
{
	/************* 打开日志 */
	if ((fp = fopen(LOG_PATH, "at")) == NULL)
		return FALSE;

    semid = sem_process_init(CLIENT_KEY_PATH, 'a');

	return TRUE;
}


/**
  * @brief		记录日志
  * @param		arg1:level  arg2：format
  * @retval		none
  */
void write_log(const char *level, const char *format, ...)
{
	time_t lt;
    struct tm *date_time;
   // struct timeval tv; //用户添加时间戳来更精确的显示时间      //&sodf12sadfos@#
    char fmtTimeBuffer[CLIENT_LOG_BUFLEN] = {0};
    va_list args;

	va_start(args, format);
    if (fp == NULL)
    {
        if (open_log(LOG_PATH) == FALSE)
        {
            return;
        }
    }

    sem_process_wait(semid);

    lt = time(NULL);
    date_time = localtime(&lt);
    strftime(fmtTimeBuffer, 30, "%x %X\n", date_time);

//    ///在这个后面加入时间戳，从1970 年算起的毫秒数
//    gettimeofday(&tv,NULL);            //&sodf12sadfos@#
//    snprintf(fmtTimeBuffer+strlen(fmtTimeBuffer)-1,  CLIENT_LOG_BUFLEN-strlen(fmtTimeBuffer)-1, " %ld ", tv.tv_sec*1000 + tv.tv_usec/1000);
//    ///在这个后面加入时间戳，从1970 年算起的毫秒数

    snprintf(fmtTimeBuffer+strlen(fmtTimeBuffer)-1,  CLIENT_LOG_BUFLEN-strlen(fmtTimeBuffer)-1, "  %s  ", level);
    vsnprintf(fmtTimeBuffer+strlen(fmtTimeBuffer)-1, CLIENT_LOG_BUFLEN-strlen(fmtTimeBuffer)-1, format, args);
    snprintf(fmtTimeBuffer+strlen(fmtTimeBuffer),    CLIENT_LOG_BUFLEN-strlen(fmtTimeBuffer)-1, "\n");

    fputs(fmtTimeBuffer, fp);
	fflush(fp);

	sem_process_post(semid);

    if (streql(level, LOG_FATAL))
    {
        exit(EXIT_FAILURE);
    }
}

/**
  * @brief  断言失败
  * @param  NULL
  * @retval void
  */
void assert_fail(char *expr, char *file, int line)
{
    MSG(LOG_FATAL, "%s%s %s%s%d", "Assertion failed: ", expr ,file, ", line ", line);
}

/**
  * @brief  关闭日志
  * @param  NULL
  * @retval void
  */
void close_log()
{
    if (fp)
    {
        fclose(fp);
        fp = NULL;
        sem_process_delete(semid);
    }
}

/**
  * @brief  移除日志
  * @param  NULL
  * @retval void
  */
void remove_log()
{
	if (access(LOG_PATH, 0) != 0)
		return;
	remove(LOG_PATH);
}

/**
  * @brief  计算文件的大小(M)
  * @param  NULL
  * @retval 文件大小
  */
static int get_file_size()
{
    FILE *fp = NULL;
    unsigned long file_size;

    if((fp=fopen(LOG_PATH, "rt")) == NULL)
    {
        return -1;
    }                               //if 悬挂
	fseek(fp, 0, SEEK_END);
	file_size = ftell(fp);
	fclose(fp);

	return file_size / 1024;
}

/**
  * @brief  清理大于3M的日志
  * @param  NULL
  * @retval void
  */
void clean_log()
{
	if (access(LOG_PATH, 0) != 0)
		return;
	if (get_file_size(LOG_PATH) > 3000)
		remove(LOG_PATH);
}


/******************* (C) COPYRIGHT 2020 上海今中网络科技有限公司 *****END OF FILE****/

